package com.yc.aspects;

import com.yc.bean.Account;
import com.yc.bean.MessageBean;
import com.yc.service.BankBiz;
import com.yc.service.EmailBiz;
import com.yc.service.JmsMessageProducer;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.io.StringWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author zwei
 * @create 2024-08-10 17:00
 */
@Aspect         //切面类
@Component
@Order(10)
public class BankBizEmailAspect {
    //切入点表达式
    @Pointcut("execution(* com.yc.service.BankBizImpl.deposite(..))")   //匹配业务层更新操作
    public void deposite() {

    }
    @Pointcut("execution(* com.yc.service.BankBizImpl.withdraw(..))")   //匹配业务层更新操作
    public void withdraw() {

    }
    @Pointcut("execution(* com.yc.service.BankBizImpl.transfer(..))")   //匹配业务层更新操作
    public void transfer() {

    }
    @Pointcut("deposite() || withdraw() || transfer()")
    private void all(){
    }
//    @Autowired
//    private EmailBiz emailBiz;
    @Autowired
    private BankBiz bankBiz;
//    @Autowired
//    private VelocityEngine velocityEngine;  //注入模块引擎
//    @Autowired
//    private VelocityContext context;  //注入模板上下文
//    @Autowired
//    @Qualifier("depositTemplate")
//    private Template depositeTemplete;
//    @Autowired
//    @Qualifier("withdrawTemplate")
//    private Template withdrawTemplete;
//    @Autowired
//    @Qualifier("transferTemplate")
//    private Template transferTemplete;

//    @Autowired
//    @Qualifier("fullDf")
//    private DateFormat fullDf;
//
//    @Autowired
//    @Qualifier("partDf")
//    private DateFormat partDf;
    @Autowired
    private JmsMessageProducer jmsMessageProducer;
    //增强类型：后置(after，afterreturning，finally)  前置 异常 around
    @AfterReturning("all()")
    public void sendEmail(JoinPoint jp){
        Object[] objs = jp.getArgs();
        int accountid = Integer.parseInt( objs[0].toString()  );    //第一个参数
        double money =  Double.parseDouble(objs[1].toString());     //第二个参数
        //判断jp实发转账操作
        int toaccountid;
        if (jp.getSignature().getName().equals("transfer.vm")){
            toaccountid = Integer.parseInt( objs[2].toString()  );    //第三个参数
        } else {
            toaccountid = 0;
        }
        Account account = bankBiz.findAccount(accountid);
        String email=account.getEmail();
        String info;
        String mothodName=jp.getSignature().getName();
        new Thread(  ()->{
            jmsMessageProducer.sendMessage(  new MessageBean(account,toaccountid,email,money,mothodName));
        } ).start();


//        if (mothodName.equals("deposite")){
//            info=depositeInfo(account, money);
//        } else if (mothodName.equals("withdraw")) {
//            info=withdrawInfo(account, money);
//        } else if(mothodName.equals("transfer")){
//            info=transferInfo(account, money, toaccountid);
//        }else {
//            info="";
//        }
//        new Thread( ()->{
//            try {
//                emailBiz.sendMail(email, "账户变动通知", info);
//            } catch (Exception e) {
//                throw new RuntimeException(e);
//            }
//        }).start();
    }


    //以下发送邮件内容交给一个单独的消费模块

//    private String transferInfo(Account account, double money, int toaccountid) {
////        Date d=new Date();
////        DateFormat df=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
////        String dstring=df.format(d);
////        String s="尊敬的%s先生:\n\t您的账户%s于%s向账户%s转账了%s元,当前余额%s元\n\t\t\t\t飞少银行总行";
////        return String.format(s,account.getAccountid(),account.getAccountid(),dstring,toaccountid,money,account.getBalance());
//        Date d=new Date();
////        DateFormat fullDf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
////        DateFormat partDf=new SimpleDateFormat("yyyy年MM月dd日 北京时间hh时");
////
////        //后面使用spring托管
////        VelocityEngine velocityEngine=new VelocityEngine(); //spring未托管  需要new
////        velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER,"classpath");   //设置从什么位置加载模板vm
////        velocityEngine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
////        velocityEngine.init();
//
//        //托管
////        VelocityContext context=new VelocityContext();  //模板上下文 用于存占位符的值
//        context.put("accountid",account.getAccountid());
//        context.put("email",account.getEmail());
//        context.put("subject","存款操作通知");
//        context.put("optime",fullDf.format(d));
//        context.put("money",money);
//        context.put("balance",account.getBalance());
//        context.put("currentDate",partDf.format(d));
//        //Merge template and context
////        Template template=velocityEngine.getTemplate("vms/transfer.vm","utf-8");
//        try {
//            StringWriter writer=new StringWriter(); //合并内容 替换占位符
//            transferTemplete.merge(context,writer);         //从流获取最终的字符串
//            return writer.toString();
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        return "";
//    }

//    private String withdrawInfo(Account account, double money) {
////        Date d=new Date();
////        DateFormat df=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
////        String dstring=df.format(d);
////        String s="尊敬的%s先生:\n\t您的账户%s于%s取出了%s元,当前余额%s元\n\t\t\t\t飞少银行总行";
////        return String.format(s,account.getAccountid(),account.getAccountid(),dstring,money,account.getBalance());
//        Date d=new Date();
////        DateFormat fullDf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
////        DateFormat partDf=new SimpleDateFormat("yyyy年MM月dd日 北京时间hh时");
//
//        //后面使用spring托管
////        VelocityEngine velocityEngine=new VelocityEngine(); //spring未托管  需要new
////        velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER,"classpath");   //设置从什么位置加载模板vm
////        velocityEngine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
////        velocityEngine.init();
//
//        //托管
////        VelocityContext context=new VelocityContext();  //模板上下文 用于存占位符的值
//        context.put("accountid",account.getAccountid());
//        context.put("email",account.getEmail());
//        context.put("subject","取款操作通知");
//        context.put("optime",fullDf.format(d));
//        context.put("money",money);
//        context.put("balance",account.getBalance());
//        context.put("currentDate",partDf.format(d));
//
//        //Merge template and context
////        Template template=velocityEngine.getTemplate("vms/withdraw.vm","utf-8");
//        try {
//            StringWriter writer=new StringWriter(); //合并内容 替换占位符
//            withdrawTemplete.merge(context,writer);         //从流获取最终的字符串
//            return writer.toString();
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        return "";
//    }

//    private String depositeInfo(Account account, double money){
//        //拼接文档
////        Date d=new Date();
////        DateFormat df=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
////        String dstring=df.format(d);
////        String s="尊敬的%s先生:\n\t您的账户%s于%s存入了%s元,当前余额%s元\n\t\t\t\t飞少银行总行";
////        return String.format(s,account.getAccountid(),account.getAccountid(),dstring,money,account.getBalance());
//        Date d=new Date();
////        DateFormat fullDf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
////        DateFormat partDf=new SimpleDateFormat("yyyy年MM月dd日 北京时间hh时");
//
//        //后面使用spring托管
////        VelocityEngine velocityEngine=new VelocityEngine(); //spring未托管  需要new
////        velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER,"classpath");   //设置从什么位置加载模板vm
////        velocityEngine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
////        velocityEngine.init();
//
//        //托管
////        VelocityContext context=new VelocityContext();  //模板上下文 用于存占位符的值
//        context.put("accountid",account.getAccountid());
//        context.put("email",account.getEmail());
//        context.put("subject","存款操作通知");
//        context.put("optime",fullDf.format(d));
//        context.put("money",money);
//        context.put("balance",account.getBalance());
//        context.put("currentDate",partDf.format(d));
//
//        //Merge template and context
////        Template template=velocityEngine.getTemplate("vms/deposite.vm","utf-8");
//        try {
//            StringWriter writer=new StringWriter(); //合并内容 替换占位符
//            depositeTemplete.merge(context,writer);         //从流获取最终的字符串
//            return writer.toString();
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        return "";
//    }
}
